#coding=utf-8
'''可多开，可最小化到托盘，可快捷键隐藏

'''
import wx
from wx.lib import iewin
import wxHelper
import dmsoft
#TODO:不响应“显示桌面”或win + D
class TitleDialog(wx.Dialog):
    def __init__(self):
        wx.Dialog.__init__(self, None, -1, u'设置标题', size=(180, 100))
        self.lblTitle, self.txtTitle = wxHelper.getLblAndTxt(self, u'标题')
        self.btnOk = wx.Button(self, wx.ID_OK, u'设置')
        self.btnCanel = wx.Button(self, wx.ID_CANCEL, u'取消')
        self.layout()

    def layout(self):
        self.Center()    
        titleSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
        titleSizer.AddGrowableCol(1)
        titleSizer.Add(self.lblTitle, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        titleSizer.Add(self.txtTitle, 0, wx.EXPAND)

        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(self.btnOk, 0, wx.ALL, 5)
        btnSizer.Add(self.btnCanel, 0, wx.ALL, 5)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(titleSizer, 0, wx.ALL, 5)
        mainSizer.Add(btnSizer, 0, wx.ALL, 5)

        self.SetSizer(mainSizer)

class HotkeyDialog(wx.Dialog):
    def __init__(self, hotkey1, hotkey2):
        wx.Dialog.__init__(self, None, -1, u'设置最小化快捷键', size=(200, 100))
        self.lblHotkey, self.chHotkey1, self.chHotkey2 = wxHelper.getHotkeyControl(self, hotkey1, hotkey2)
        self.btnOk = wx.Button(self, wx.ID_OK, u'设置')
        self.btnCanel = wx.Button(self, wx.ID_CANCEL, u'取消')
        self.layout()

    def layout(self):
        self.Center()    
        titleSizer = wx.FlexGridSizer(cols=3, hgap=5, vgap=5)
        titleSizer.Add(self.lblHotkey, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        titleSizer.Add(self.chHotkey1, 0, wx.EXPAND)
        titleSizer.Add(self.chHotkey2, 0, wx.EXPAND)

        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(self.btnOk, 0, wx.ALL, 5)
        btnSizer.Add(self.btnCanel, 0, wx.ALL, 5)

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(titleSizer, 0, wx.ALL, 5)
        mainSizer.Add(btnSizer, 0, wx.ALL, 5)

        self.SetSizer(mainSizer)
class TaskBarIcon(wx.TaskBarIcon):
    def __init__(self, frame, icon_name, app_name='app'):
        wx.TaskBarIcon.__init__(self)
        self.frame = frame
        self.app_name = app_name
        self.icon = self.makeIcon(wx.Image((icon_name), wx.BITMAP_TYPE_ICO))
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_TASKBAR_RIGHT_DOWN, self.OnContextMenu)

    def makeIcon(self, img):
        img = img.Scale(16, 16)
        icon = wx.IconFromBitmap(img.ConvertToBitmap())
        return icon

    def OnLeftDown(self, event):
        self.OnRestore(event)

    def OnRestore(self, event):
        self.frame.show()
        self.RemoveIcon()
        
    def OnQuit(self, event):
        self.frame.Destroy()
        self.Destroy()
        
    def OnContextMenu(self, event):
        if not hasattr(self, "popupID1"):
            self.popupID1 = wx.NewId()
            self.popupID2 = wx.NewId()
            self.Bind(wx.EVT_MENU, self.OnRestore, id=self.popupID1)
            self.Bind(wx.EVT_MENU, self.OnQuit, id=self.popupID2)
        menu = wx.Menu()
        item1 = wx.MenuItem(menu, self.popupID1, u"恢复")
        item2 = wx.MenuItem(menu, self.popupID2, u"退出")
        menu.AppendItem(item1)
        menu.AppendItem(item2)
        self.PopupMenu(menu)
        menu.Destroy()

    def set(self):
        self.SetIcon(self.icon,  self.app_name)

class MainFrame(wx.Frame):
    """已经提供了log，dm
    """
    def __init__(self, log, name=u'网页游戏专用浏览器',icon_name='bin.ico', **kargs):
        wx.Frame.__init__(self, None, -1, name, style=wx.DEFAULT_FRAME_STYLE|wx.FRAME_TOOL_WINDOW)
        self.log = log
        self.icon_name = icon_name
        self.app_name = name
        self.setToolBar()
        self.setUrlBar()
        self.setIE()
        self.setIcon()
        self.doLayout()

        #TODO:每个小号一个配置文件，存储一个快捷键，初始化为alt + q
        self.hotKeyId_min = 100
        self.hotkey1_min, self.hotkey2_min = 'Alt', 'q'
        self.registerMinHotkey()
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyMin, id=self.hotKeyId_min)

        self.position = self.GetPosition()#记录窗口位置
        self.far = 10000#遥远的地方，算是隐藏了
        self.dm = dmsoft.Dmsoft(log)
        self.dm.hwnd = self.GetHandle()#获取自身hwnd


    def setToolBar(self):
        self.btnMin = wx.Button(self, -1, u'最小化')
        self.Bind(wx.EVT_BUTTON, self.OnMinClick, self.btnMin)  
        self.btnHotkey= wx.Button(self, -1, u'设置热键')
        self.Bind(wx.EVT_BUTTON, self.OnHotkeyClick, self.btnHotkey)  
        self.btnTitle = wx.Button(self, -1, u'设置标题')
        self.Bind(wx.EVT_BUTTON, self.OnTitleClick, self.btnTitle)  
        self.btnMainWeb = wx.Button(self, -1, u'官方网站')
        self.Bind(wx.EVT_BUTTON, self.OnMainWebClick, self.btnMainWeb)  

    def setUrlBar(self):
        self.btnRefresh = wx.Button(self, -1, u'刷新')
        self.Bind(wx.EVT_BUTTON, self.OnRefreshClick, self.btnRefresh)  
        self.lblUrl = wx.StaticText(self, -1, u'网址:')
        self.txtUrl = wx.TextCtrl(self, -1, u'http://')
        self.btnLoad = wx.Button(self, -1, u'进入')
        self.Bind(wx.EVT_BUTTON, self.OnLoadClick, self.btnLoad)  

    def setIE(self):
        self.ie = wx.lib.iewin.IEHtmlWindow(self)

    def setIcon(self):
        self.tbIcon = TaskBarIcon(self, self.icon_name, self.app_name)

    def doLayout(self):
        self.Maximize()
        self.toolSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.toolSizer.Add(self.btnMin, 0, wx.ALL, 5)
        self.toolSizer.Add(self.btnHotkey, 0, wx.ALL, 5)
        self.toolSizer.Add(self.btnTitle, 0, wx.ALL, 5)
        self.toolSizer.Add(self.btnMainWeb, 0, wx.ALL, 5)

        self.urlSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.urlSizer.Add(self.btnRefresh, 0, wx.ALL, 5)
        self.urlSizer.Add(self.lblUrl, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
        self.urlSizer.Add(self.txtUrl, 1, wx.ALL, 5)
        self.urlSizer.Add(self.btnLoad, 0, wx.ALL, 5)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.mainSizer.Add(self.toolSizer, 0)
        self.mainSizer.Add(self.urlSizer, 0, wx.EXPAND)
        self.mainSizer.Add(self.ie, 1, wx.EXPAND)

        self.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self)

    def OnMinClick(self, evt):
        self.hide()
        self.tbIcon.set()

    def OnHotKeyMin(self, evt):
        if self.isHide():
            self.show()
        else:
            self.hide()

    def OnHotkeyClick(self, evt):
        dialog = HotkeyDialog(self.hotkey1_min, self.hotkey2_min)
        result = dialog.ShowModal()
        if result  ==  wx.ID_OK:
            self.hotkey1_min = dialog.chHotkey1.GetStringSelection()
            self.hotkey2_min = dialog.chHotkey2.GetStringSelection()
            if self.registerMinHotkey():
                wxHelper.msg(u'快捷键注册成功:%s + %s' %(self.hotkey1_min, self.hotkey2_min))
            else:
                wxHelper.msg(u'快捷键注册失败')

    def OnTitleClick(self, evt):
        dialog = TitleDialog()
        result = dialog.ShowModal()
        if result  ==  wx.ID_OK:
            title = dialog.txtTitle.GetValue()
            self.SetTitle(title)

    def OnMainWebClick(self, evt):
        url = config.main_web
        self.txtUrl.SetValue(url)
        self.ie.LoadUrl(url)

    def OnRefreshClick(self, evt):
        self.ie.RefreshPage()

    def OnLoadClick(self, evt):
        url = self.txtUrl.GetValue().strip()
        if not url.startswith('http://'):
            url = 'http://' + url
            self.txtUrl.SetValue(url)
        self.ie.LoadUrl(url)
                
    def show(self):
        self.SetPosition(self.postion)
        self.dm.activeWindow()

    def hide(self):
        self.postion = self.GetPosition()
        self.SetPosition((self.far, self.far))

    def isHide(self):
        postion = self.GetPosition()
        return postion[0] == self.far

    def registerMinHotkey(self):
        self.UnregisterHotKey(self.hotKeyId_min)
        choice1, choice2 = wxHelper.getHotkey(self.hotkey1_min, self.hotkey2_min)
        return self.RegisterHotKey(self.hotKeyId_min, choice1, choice2)
if __name__ == '__main__':
    import config
    app = wx.PySimpleApp()
    main = MainFrame(config.log)
    main.Show()
    app.MainLoop()

    #app = wx.PySimpleApp()
    #app.MainLoop()
    #HotkeyDialog('Alt', '1').ShowModal()
